package com.semidata.trp.decision.business;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.semidata.rtd.core.iplib.Area;
import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.message.business.Business;

public class Resource {

    private int id;
    private Position position;
    private Set<Area> areas;
    private Set<Integer> hours;
    private List<Strategy> strategies;
    private double floor;
    private boolean isAllowShared;
    public Resource(Business.Resource data) {
        this.id = data.getId();
        this.position = null;
        this.floor = data.getFloor();
        this.strategies = new ArrayList<>();
        this.areas = new HashSet<>();
        this.hours = new HashSet<>();
    }

    public String toString() {

        StringBuffer sb = new StringBuffer();

        sb.append("Resource ").append(id).append('\n');

        for (Strategy strategy : strategies) {
            sb.append(strategy.toString());
        }

        return sb.toString();
    }
    
    public int getId() {
        return id;
    }
    
    public boolean isAllowShared() {
		return isAllowShared;
	}

	public void setAllowShared(boolean isAllowShared) {
		this.isAllowShared = isAllowShared;
	}

	public void setPosition(Position position) {
        this.position = position;
    }

    public Position getPosition() {
        return position;
    }
    
    public Set<Area> getAreas() {
		return areas;
	}

	public void setAreas(Set<String> areas) {
		for (String code : areas)
			this.areas.add(new Area(code));	
	}

	public Set<Integer> getHours() {
		return hours;
	}

	public void setHours(Set<Integer> hours) {
		this.hours = hours;
	}

	public void addStrategy(Strategy strategy) {
		Logger.log("DEBUG", "resource id :" + this.getId() + " add a strategy id : " + strategy.getId());
        this.strategies.add(strategy);
    }

    public List<Strategy> getStrategies() {
        return strategies;
    }
    
    public double getFloor() {
    	return this.floor;
    }

    public void sort() {
        Collections.sort(strategies, new Comparator<Strategy>() {

            @Override
            public int compare(Strategy s1, Strategy s2) {
                int c = s2.getPriority() - s1.getPriority();
                if (c != 0)
                    return c;
                c = (int) Math.signum(s2.getPrice() - s1.getPrice());
                return c;
            }
        });

    }

}
